package com.nstar.base.util
{
	import mx.containers.Form;
	import mx.containers.FormItem;
	import flash.utils.getQualifiedClassName;
	import mx.core.Application;
	
	public class PostUtils
	{
		
		
		public static function output(array:Array):String {
			var output:String = "";
			
			for(var i=0,j=array.length;i<j;i++) {
				var form:Form = array[i];
				for each(var formItem:FormItem in form.getChildren()) {
					var child = formItem.getChildAt(0);
					var className:String = getQualifiedClassName(child)
					if(className == "mx.controls::TextInput") {
						add(child.name+"="+child.text);
					}
					else {
						add(child.name+"="+child.value);
					}
					//trace(child.name+"="+getQualifiedClassName(child));
				}
			}
			
				
			return output;
			
			function add(str:String) {
				output += "&"+str;
			}
		}
		
		public static function generateMap(array:Array):Object {
			var result:Object = new Object();
			
			for(var i=0,j=array.length;i<j;i++) {
				var form:Form = array[i];
				for each(var formItem:FormItem in form.getChildren()) {
					var child = formItem.getChildAt(0);
					var className:String = getQualifiedClassName(child)
					
					//对于TextInput特殊取值text,其余取value
					if(className == "mx.controls::TextInput") {
						if(child.name && child.text)
							result[child.id] = child.text;
					}
					else {
						if(child.name && child.value)
							result[child.id] = child.value;
					}
				}
			}
				
			return result;
			
			
		}
		
		/**
		 * 获取HTML中保存的数据
		 */
		private static var _model:XML;
		
		public static function get model():XML {
			if (_model == null) {
				var strModel:String = unescape( Application.application.parameters.model ) ;
				var start:uint = strModel.indexOf( "<!--FXTMODEL-->" )+15;
				var end:uint = strModel.lastIndexOf( "<!--FXTMODEL-->") ;
				
				_model = new XML( strModel.substring( start , end ) );
			}
			
			return _model;
			
		}
		
		
	}
}